import { get, post, put, del } from '@/utils/request'

// 获取商户设置
export function getMerchantSettings() {
  return get('/settings/merchant')
}

// 更新商户设置
export function updateMerchantSettings(settings) {
  return put('/settings/merchant', settings, {
    showLoading: true,
    loadingText: '保存设置中...'
  })
}

// 获取营业设置
export function getBusinessSettings() {
  return get('/settings/business')
}

// 更新营业设置
export function updateBusinessSettings(settings) {
  return put('/settings/business', settings)
}

// 获取风控设置
export function getRiskSettings() {
  return get('/settings/risk')
}

// 更新风控设置
export function updateRiskSettings(settings) {
  return put('/settings/risk', settings)
}

// 获取通知设置
export function getNotificationSettings() {
  return get('/settings/notification')
}

// 更新通知设置
export function updateNotificationSettings(settings) {
  return put('/settings/notification', settings)
}

// 获取界面设置
export function getInterfaceSettings() {
  return get('/settings/interface')
}

// 更新界面设置
export function updateInterfaceSettings(settings) {
  return put('/settings/interface', settings)
}

// 获取服务区域设置
export function getServiceAreas() {
  return get('/settings/service-areas')
}

// 更新服务区域设置
export function updateServiceAreas(areas) {
  return put('/settings/service-areas', areas)
}

// 添加服务区域
export function addServiceArea(area) {
  return post('/settings/service-areas', area)
}

// 更新服务区域
export function updateServiceArea(areaId, area) {
  return put(`/settings/service-areas/${areaId}`, area)
}

// 删除服务区域
export function deleteServiceArea(areaId) {
  return del(`/settings/service-areas/${areaId}`)
}

// 获取营业时间设置
export function getWorkingHours() {
  return get('/settings/working-hours')
}

// 更新营业时间设置
export function updateWorkingHours(hours) {
  return put('/settings/working-hours', hours)
}

// 获取配送设置
export function getDeliverySettings() {
  return get('/settings/delivery')
}

// 更新配送设置
export function updateDeliverySettings(settings) {
  return put('/settings/delivery', settings)
}

// 重置设置为默认值
export function resetSettingsToDefault() {
  return post('/settings/reset-default')
}

// 导出设置配置
export function exportSettings() {
  return get('/settings/export', {}, {
    responseType: 'blob'
  })
}

// 导入设置配置
export function importSettings(file) {
  const formData = new FormData()
  formData.append('file', file)
  return post('/settings/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取系统默认设置
export function getDefaultSettings() {
  return get('/settings/defaults')
}

// 验证设置配置
export function validateSettings(settings) {
  return post('/settings/validate', settings)
}

// 获取设置变更历史
export function getSettingsHistory(params = {}) {
  return get('/settings/history', params)
}

// 恢复历史设置
export function restoreHistorySettings(historyId) {
  return post(`/settings/restore/${historyId}`)
}

// 获取设置模板
export function getSettingsTemplates() {
  return get('/settings/templates')
}

// 应用设置模板
export function applySettingsTemplate(templateId) {
  return post(`/settings/templates/${templateId}/apply`)
}

// 保存为设置模板
export function saveAsSettingsTemplate(name, description, settings) {
  return post('/settings/templates', {
    name,
    description,
    settings
  })
}

// 主题模式常量
export const THEME_MODES = {
  LIGHT: 'light',
  DARK: 'dark',
  AUTO: 'auto'
}

export const THEME_MODE_TEXT = {
  [THEME_MODES.LIGHT]: '白天模式',
  [THEME_MODES.DARK]: '夜间模式',
  [THEME_MODES.AUTO]: '跟随系统'
}

// 字体大小常量
export const FONT_SIZES = {
  SMALL: 'small',
  MEDIUM: 'medium',
  LARGE: 'large'
}

export const FONT_SIZE_TEXT = {
  [FONT_SIZES.SMALL]: '小号',
  [FONT_SIZES.MEDIUM]: '标准',
  [FONT_SIZES.LARGE]: '大号'
}

// 通知方式常量
export const NOTIFICATION_METHODS = {
  APP: 'app',
  SMS: 'sms',
  EMAIL: 'email'
}

export const NOTIFICATION_METHOD_TEXT = {
  [NOTIFICATION_METHODS.APP]: 'APP推送',
  [NOTIFICATION_METHODS.SMS]: '短信通知',
  [NOTIFICATION_METHODS.EMAIL]: '邮件通知'
}

// 护眼模式等级常量
export const EYE_PROTECTION_LEVELS = {
  LEVEL_1: 1,
  LEVEL_2: 2,
  LEVEL_3: 3,
  LEVEL_4: 4,
  LEVEL_5: 5
}

export const EYE_PROTECTION_LEVEL_TEXT = {
  [EYE_PROTECTION_LEVELS.LEVEL_1]: '轻度',
  [EYE_PROTECTION_LEVELS.LEVEL_2]: '适中',
  [EYE_PROTECTION_LEVELS.LEVEL_3]: '标准',
  [EYE_PROTECTION_LEVELS.LEVEL_4]: '强化',
  [EYE_PROTECTION_LEVELS.LEVEL_5]: '最强'
}

// 营业状态常量
export const BUSINESS_STATUS = {
  OPEN: 'open',
  CLOSED: 'closed',
  BREAK: 'break',
  MAINTENANCE: 'maintenance'
}

export const BUSINESS_STATUS_TEXT = {
  [BUSINESS_STATUS.OPEN]: '营业中',
  [BUSINESS_STATUS.CLOSED]: '已打烊',
  [BUSINESS_STATUS.BREAK]: '休息中',
  [BUSINESS_STATUS.MAINTENANCE]: '维护中'
}

// 获取主题描述
export function getThemeDescription(theme) {
  const descriptions = {
    [THEME_MODES.LIGHT]: '明亮清爽的界面，适合白天使用',
    [THEME_MODES.DARK]: '深色护眼界面，适合夜间使用',
    [THEME_MODES.AUTO]: '根据系统时间自动切换主题模式'
  }
  return descriptions[theme] || ''
}

// 获取护眼等级描述
export function getEyeProtectionLevelDescription(level) {
  const descriptions = {
    [EYE_PROTECTION_LEVELS.LEVEL_1]: '轻微降低蓝光',
    [EYE_PROTECTION_LEVELS.LEVEL_2]: '适度降低蓝光',
    [EYE_PROTECTION_LEVELS.LEVEL_3]: '标准护眼效果',
    [EYE_PROTECTION_LEVELS.LEVEL_4]: '强化护眼效果',
    [EYE_PROTECTION_LEVELS.LEVEL_5]: '最强护眼效果'
  }
  return descriptions[level] || ''
}

// 验证营业时间
export function validateWorkingHours(startTime, endTime) {
  if (!startTime || !endTime) {
    return { valid: false, message: '请设置完整的营业时间' }
  }
  
  const start = new Date(`2000-01-01 ${startTime}`)
  const end = new Date(`2000-01-01 ${endTime}`)
  
  if (start >= end) {
    return { valid: false, message: '结束时间必须晚于开始时间' }
  }
  
  return { valid: true }
}

// 验证服务区域
export function validateServiceArea(area) {
  if (!area.name || area.name.trim().length === 0) {
    return { valid: false, message: '请输入区域名称' }
  }
  
  if (!area.radius || area.radius <= 0) {
    return { valid: false, message: '请设置有效的服务半径' }
  }
  
  if (area.radius > 50) {
    return { valid: false, message: '服务半径不能超过50公里' }
  }
  
  return { valid: true }
}

// 计算配送费用
export function calculateDeliveryFee(distance, freeDistance, feePerKm) {
  if (distance <= freeDistance) {
    return 0
  }
  return Math.ceil((distance - freeDistance) * feePerKm * 100) / 100
}
